import { Body, Controller, Get, Param, Post } from "@nestjs/common";
import { WalletService } from "./wallet.service";
import { AccountDto, Transaction, WalletDto } from "./dto";

@Controller('wallet')
export class WalletController {

  constructor(private walletService: WalletService) {}

  @Post('/createWallet')
  async createWallet(@Body() walletDto: WalletDto) {
    const wallet = this.walletService.createWallet(walletDto.network,walletDto.password)
    return wallet;
  }

  @Post('/recoverWallet')
  async recoverWallet(@Body() walletDto: WalletDto) {
    const wallet = this.walletService.recoverWallet(walletDto.network,walletDto.mnemonic,walletDto.password);
    return wallet;
  }

  @Post('/createAccount')
  async createAccount(@Body() accountDto: AccountDto) {
    const account = this.walletService.createAccount(accountDto.index)
    return account;
  }

  @Post('/setNetwork')
  async setNetwork(@Body('network') network: string) {
    console.log("--------network=",network);
    this.walletService.setNetwork(network);
  }

  @Post('/sendTransaction')
  async sendTransaction(@Body() transaction: Transaction) {
    this.walletService.sendTransaction(transaction.to,transaction.amount);
  }

  @Get('/getBitcoinBlockHeight')
  async getBitcoinBlockHeight() {
    const height = this.walletService.getBitcoinBlockHeight()
    return height;
  }

  @Get('/getPolygonBalance')
  async getPolygonBalance() {
    const balance = this.walletService.getBalance()
    return balance;
  }

/*  @Post('/createChildNode')
  async createChildNode(@Body() walletDto: WalletDto): Promise<string> {
    const seed = bip39.mnemonicToSeedSync(walletDto.mnemonic,walletDto.password)
    const bip32 = BIP32Factory(ecc);
    const rootNode = bip32.fromSeed(seed);
    let childNode;
    let privateKey;
    let address;
    switch (walletDto.network) {
      case "ethereum":
        childNode = rootNode.derivePath(`m/44'/60'/0'/0/0`);
        privateKey = '0x'+childNode.privateKey.toString("hex");
        address = computeAddress(privateKey);
        break;
      case "arbitrum":
        childNode = rootNode.derivePath(`m/44'/9001'/0'/0/0`);
        privateKey = '0x'+childNode.privateKey.toString("hex");
        address = computeAddress(privateKey);
        break;
      case "matic":
        childNode = rootNode.derivePath(`m/44'/966'/0'/0/0`);
        privateKey = '0x'+childNode.privateKey.toString("hex");
        address = computeAddress(privateKey);
        break;
      default:
        throw new Error('Invalid network');
        break;
    }
    console.log("-------------privateKey=",privateKey);
    console.log("-------------address=",address);
    return privateKey;
  }*/


}
